package com.ziguang.redwood.websocket.chat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.RemoteEndpoint.Basic;

public class ChatRoom 
{
	private static ChatRoom room = new ChatRoom();
	
	
	private final ConcurrentHashMap<String,List<ChatEndpoint>> rooms = new ConcurrentHashMap<>();
	
	private ChatRoom()
	{
		
	}
	
	public static ChatRoom getInstance()
	{
		return room;
	}
	
	//加入到
	public void join(ChatEndpoint endpoint)
	{
		List<String> roomIds = endpoint.getRoomIds();
		for(String roomId : roomIds)
		{
			rooms.putIfAbsent(roomId, new ArrayList<ChatEndpoint>());
			List<ChatEndpoint> endPointSet = rooms.get(roomId);
			endPointSet.add(endpoint);
		}
		
		Response response = new Response();
		response.setType(Response.JOIN_RESPONSE);
		response.setSender(endpoint.getSender());
		response.setRooms(list2Str(roomIds));
		brocastAll(roomIds,response);
		
	}
	
	
	public void msgBrocast(String sender,String roomId,String content, String niceName)
	{
		Response response = new Response();
		response.setType(Response.CHAT_RESPONSE);
		response.setSender(sender);
		response.setRooms(roomId);
		response.setBody(content);
		response.setNiceName(niceName);
		brocast(roomId,response);
		
	}
	
	
	public void quit(ChatEndpoint endpoint)
	{
		List<String> roomIds = endpoint.getRoomIds();
		for(String id : roomIds)
		{
			rooms.get(id).remove(endpoint);
		}
		Response response = new Response();
		response.setType(Response.QUIT_RESPONSE);
		response.setSender(endpoint.getSender());
		response.setRooms(list2Str(roomIds));
		brocastAll(roomIds,response);
	}
	
	public void getOnlineUsers(ChatEndpoint endpoint,String roomId)
	{
		List<ChatEndpoint> endPointSet = rooms.get(roomId);
		int i = 0;
		StringBuilder sb = new StringBuilder();
		for(ChatEndpoint ep : endPointSet)
		{
			if(i != 0)
			{
				sb.append(",");
			}
			sb.append(ep.getSender());
			i++;
		}
		
		Response response = new Response();
		response.setType(Response.ONlINE_USER_RESPONSE);
		response.setRooms(roomId);
		response.setBody(sb.toString());
		response.setSender(endpoint.getSender());
		String str = Coder.encode(response);
		try
		{
			Basic basic = endpoint.getSession().getBasicRemote();
			basic.sendText(str);
		}
		catch(IOException ex)
		{
			List<String> roomIds = endpoint.getRoomIds();
			for(String id : roomIds)
			{
				rooms.get(id).remove(endpoint);
			}
		}
		
	}
	
	
	private String list2Str(List<String> roomIds)
	{
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < roomIds.size(); i++)
		{
			if(i != 0)
			{
				sb.append(",");
			}
			sb.append(roomIds.get(i));
		}
		return sb.toString();
	}
	
	private void brocastAll(List<String> roomIds,Response response)
	{
		for(String roomId : roomIds)
		{
			brocast(roomId,response);
		}
	}
	
	private void brocast(String roomId,Response response) 
	{
		List<ChatEndpoint> endPointSet = rooms.get(roomId);
		String str = Coder.encode(response);
		for(ChatEndpoint endpoint : endPointSet)
		{
			try
			{
				Basic basic = endpoint.getSession().getBasicRemote();
				basic.sendText(str);
			}
			catch(IOException ex)
			{
				List<String> roomIds = endpoint.getRoomIds();
				for(String id : roomIds)
				{
					rooms.get(id).remove(endpoint);
				}
			}
			
		}
	}
	
	

}
